
// Definition for a Node.
function Node(val, next) {
    this.val = val;
    this.next = next;
};
function createList(arr) {
  if(arr.length == 0)
    return null;
  let head = new Node(arr[0], null), headNode = head;
  for(let i = 1; i < arr.length; i++) {
    let temp = new Node(arr[i], null);
    head.next = temp;
    head = temp;
  }
  head.next = headNode;
  return headNode;
}
function showList(head) {
  while(head) {
    console.log(head.val);
    head = head.next;
  }
}


/**
 * @param {Node} head
 * @param {number} insertVal
 * @return {Node}
 */
var insert = function(head, insertVal) {
  head = createList(head);
  if(!head) {
    head = new Node(insertVal, null);
    head.next = head;
    return head;
  }
  let temp = head;
  let last = head;
  temp = head.next;
  while(temp.val < insertVal) {
    console.log(temp);
    temp = temp.next;
    last = last.next;
  }
  let node = Node(insertVal, temp);
  last.next = node;
  return head;
};
let head = [3,4,1], insertVal = 2;
console.log(insert(head, insertVal));
head = [], insertVal = 1;
console.log(insert(head, insertVal));
head = [1], insertVal = 0;
console.log(insert(head, insertVal));
